package com.ziyougou.product.controller;

import java.util.Arrays;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.ziyougou.product.service.CategoryBrandRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.ziyougou.product.entity.BrandEntity;
import com.ziyougou.product.service.BrandService;
import com.ziyougou.common.utils.PageUtils;
import com.ziyougou.common.utils.R;
import org.springframework.web.multipart.MultipartFile;


/**
 * 品牌
 *
 * @author yuanhaipeng
 * @email yuanhaipeng@gmail.com
 * @date 2024-06-26 14:09:54
 */
@RestController
@RequestMapping("product/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 品牌logo图片上传
     */

    //文件上传  http://localhost:88/api/product/brand/fileUpload
    @PostMapping("fileUpload")
    public R fileUpload(MultipartFile file) throws Exception{
        return R.ok(brandService.fileUpload(file));
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
   // @RequiresPermissions("product:brand:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = brandService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
   // @RequiresPermissions("product:brand:info")
    public R info(@PathVariable("brandId") Long brandId){
		BrandEntity brand = brandService.getById(brandId);

        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:brand:save")
    public R save(@RequestBody BrandEntity brand){
		brandService.save(brand);

        return R.ok();
    }

    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;
    /**
     * 修改
     */
    @RequestMapping("/update")
   // @RequiresPermissions("product:brand:update")
    public R update(@RequestBody BrandEntity brand){
        //1.更新brand表
		brandService.updateById(brand);
        //2.更新CategoryBrandRelation表
        categoryBrandRelationService.updateBrandName(brand.getBrandId(),brand.getName());
        return R.ok();
    }

    @RequestMapping("/updateStatus")
    // @RequiresPermissions("product:brand:update")
    public R updateStatus(@RequestBody BrandEntity brand){
        brandService.updateStatusById(brand);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:brand:delete")
    public R delete(@RequestBody Long[] brandIds){
		brandService.removeByIds(Arrays.asList(brandIds));
        return R.ok();
    }

}
